import { defineStore } from 'pinia'
import {cloneDeep} from 'lodash'
import { getRole } from '@/utils/user';
const filterMenu = (menus: any) => {
  const role = getRole();
  if (!role) return [];
  const authMenus: any[] = [];
  menus.forEach((menu: any) => {
    if (menu.roles === '*' || menu.roles.includes(role)) {
      if (menu.children && menu.children.length > 0){
        menu.children = filterMenu(menu.children);
      }
      authMenus.push(menu);
    }
  })
  return authMenus;

}
const useBaseStore = defineStore('base', {
  state: ():any =>({
    // 侧边导航
    menus: [
      {
        label: "首页",
        path: '/dashBoard',
        roles: "*",
        icon: "Odometer"
      },
      {
        label: "商品",
        roles: ['admin', 'a', 'b'],
        icon: "Smoking",
        children: [
          {
            label: "商品管理",
            roles: ['admin', 'a', 'b'],
            path: "/itemLists"
          },
          {
            label: "商品分类管理",
            roles: ['admin', 'a', 'b'],
            path: "/cateLists"
          },
          {
            label: "商品规格",
            roles: [ 'a', 'b'],
            path: "/itemSku"
          }
        ]
      },
      {
        label: "订单",
        path: '/orderLists',
        roles: ['admin', 'a', 'b'],
        icon: "Document"
      },
      {
        label: "个人中心",
        roles: ['a', 'b'],
        path: '/userInfo',
        icon: "User"
      }
    ],
    // 侧边导航折叠状态
    menuCollapse: false
  }),
  actions: {
    toggleMenuCollapse(){
      this.menuCollapse = !this.menuCollapse
    }
  },
  getters:{
    authMenus(state: any) {
      const menus = cloneDeep(state.menus);
      return filterMenu(menus)
    }
  }
})

export default useBaseStore